package online.longzipeng.cinemasys.common.utils;

import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: lzp
 * @Date: 2021/2/8 13:49
 * 自定义异常工具类
 */
public class ExceptionUtils {

    /**
     * 不否和要求就抛出自定义异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isTrue(boolean expression,String message,Object data){
        if (!expression) {
            throw new AppException(ResponseEnum.FAILED.getCode(),message,data);
        }
    }

    public static void isTrue(boolean expression,String code,String message,Object data){
        if (!expression) {
            throw new AppException(code,message,data);
        }
    }

    public static void isTrue(boolean expression,String message){
        if (!expression) {
            throw new AppException(ResponseEnum.FAILED.getCode(),message,null);
        }
    }

    public static void isTrue(boolean expression, Result result){
        if (!expression) {
            throw new AppException(result);
        }
    }

    public static void isTrue(boolean expression, ResponseEnum responseEnum, Object data){
        if (!expression) {
            throw new AppException(responseEnum.FAILED.getCode(),responseEnum.getMessage(),data);
        }
    }

    /**
     * 如果为不为null就抛出异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isNull(Object expression,String message,Object data){
        isTrue(expression == null,message,data);
    }

    public static void isNull(List expression,String message,Object data){
        isTrue(CollectionUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NOT_NULL_OBJECT.getCode(),message,data);
    }

    public static void isNull(Object expression, ResponseEnum responseEnum, Object data){
        isTrue(expression == null,responseEnum.getCode(),responseEnum.getMessage(),data);
    }

    public static void isNull(List expression, ResponseEnum responseEnum, Object data){
        isTrue(CollectionUtils.isEmpty(expression),responseEnum.getCode(),responseEnum.getMessage(),data);
    }

    public static void isNull(Object expression,String message){
        isTrue(expression == null,ResponseEnum.EXCEPTION_NOT_NULL_OBJECT.getCode(),message,null);
    }

    public static void isNull(List expression, String message){
        isTrue(CollectionUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NOT_NULL_OBJECT.getCode(),message,null);
    }

    /**
     * 如果为null就抛出异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isNotNull(Object expression,String message,Object data){
        isTrue(expression != null,ResponseEnum.EXCEPTION_NULL_OBJECT.getCode(),message,data);
    }

    public static void isNotNull(List expression,String message,Object data){
        isTrue(!CollectionUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NULL_OBJECT.getCode(),message,data);
    }

    public static void isNotNull(Object expression, ResponseEnum responseEnum, Object data){
        if (expression == null) {
            throw new AppException(responseEnum.getCode(),responseEnum.getMessage(),data);
        }
    }

    public static void isNotNull(List expression, ResponseEnum responseEnum, Object data){
        isTrue(!CollectionUtils.isEmpty(expression),responseEnum.getCode(),responseEnum.getMessage(),data);
    }

    public static void isNotNull(Object expression,String message){
        isTrue(expression != null,ResponseEnum.EXCEPTION_NULL_OBJECT.getCode(),message,null);
    }

    public static void isNotNull(List expression,String message){
        isTrue(!CollectionUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NULL_OBJECT.getCode(),message,null);
    }

    /**
     * 如果不为空字符串就抛出异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isBlank(Object expression,String message,Object data){
        isTrue(StringUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NOT_NULL_STRING.getCode(),message,data);
    }

    public static void isBlank(Object expression, ResponseEnum responseEnum, Object data){
        isTrue(StringUtils.isEmpty(expression),responseEnum.getCode(),responseEnum.getMessage(),data);
    }

    public static void isBlank(Object expression,String message){
        isTrue(StringUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NOT_NULL_STRING.getCode(),message,null);
    }

    /**
     * 如果为null字符串就抛出异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isNotBlank(Object expression,String message,Object data){
        isTrue(!StringUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NULL_STRING.getCode(),message,data);
    }

    public static void isNotBlank(Object expression, ResponseEnum responseEnum, Object data){
        isTrue(!StringUtils.isEmpty(expression),responseEnum.getCode(),responseEnum.getMessage(),data);
    }

    public static void isNotBlank(Object expression,String message){
        isTrue(!StringUtils.isEmpty(expression),ResponseEnum.EXCEPTION_NULL_STRING.getCode(),message,null);
    }
}
